using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace DateCalc
{
    public partial class MainForm : Form
    {

        public ArrayList m_TSArray;
        

        public MainForm()
        {
            InitializeComponent();
        }

        public string FormatTimeSpan(TimeSpan ts)
        {
            const string TS_FORMAT = "{0} days {1:00}:{2:00}:{3:00}";
            return string.Format(TS_FORMAT, ts.Days, ts.Hours, ts.Minutes, ts.Seconds);

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            TimeSpan aTimeSpan;
            
            aTimeSpan = new TimeSpan((int)nupDays.Value, (int)nupHours.Value, (int)nupMins.Value, (int)nupSecs.Value);
            if (cbSign.Text == "-")
                aTimeSpan = new TimeSpan().Subtract(aTimeSpan);

            m_TSArray.Add(aTimeSpan);
            lbDates.Items.Add(FormatTimeSpan(aTimeSpan));

            lblResult.Text = FormatTimeSpan(CalcResult());
        }

        public TimeSpan CalcResult()
        {
            TimeSpan ret = new TimeSpan();
            foreach (TimeSpan ts in m_TSArray)
                ret = ret.Add(ts);
            return ret;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_TSArray = new ArrayList();
            cbSign.SelectedIndex = 0;
            lblResult.Text = FormatTimeSpan(CalcResult());
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            if (lbDates.SelectedIndex >= 0)
            {
                m_TSArray.RemoveAt(lbDates.SelectedIndex);
                lbDates.Items.RemoveAt(lbDates.SelectedIndex);
            }
            else
                MessageBox.Show("Please select the time you wanted removed.");

            lblResult.Text = FormatTimeSpan(CalcResult());
        }

        private void buttonCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(lblResult.Text);
        }
    }
}